#ifndef PROTOCOL256_H
#define PROTOCOL256_H

#include <stdint.h>
#include <stddef.h>

// CRC8计算函数声明
uint8_t calculate_crc8(const uint8_t *data, uint8_t length);

// 回调函数类型声明
typedef void (*data_callback_t)(uint8_t func_code, uint8_t *src_buff, uint8_t len, void *param);

// 协议解析器状态枚举
typedef enum {
    STATE_IDLE,
    STATE_HEADER_1,
    STATE_HEADER_2,
    STATE_FUNC_CODE,
    STATE_LENGTH,
    STATE_PAYLOAD,
    STATE_CRC
} parser_state_t;

// 协议解析器数据结构
typedef struct {
    parser_state_t state;        // 当前状态
    uint8_t buffer[1024];         // 数据缓冲区
    uint8_t buffer_index;        // 当前缓冲区索引
    uint8_t length;              // 数据域长度
    data_callback_t callback;    // 解析完成后的回调函数
    void *param;
} protocol_parser_t;

// 协议解析器初始化
void protocol256_parser_init(protocol_parser_t *parser, data_callback_t callback, void *param);

// 数据解析函数
void protocol_parse_byte(protocol_parser_t *parser, uint8_t byte);

int protocol_parse_packet(protocol_parser_t *parser,  uint8_t *packet, uint8_t length);
uint8_t protocol_pack(uint8_t func_code, const uint8_t *payload, uint8_t payload_len, uint8_t *out_buffer);
#endif // PROTOCOL256_H
